package org.example.tmsapi.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    
    // 简单的模拟用户数据库
    private static final Map<String, String> USER_DATABASE = new HashMap<>();
    
    static {
        // 添加一些测试用户
        USER_DATABASE.put("admin", "admin123");
        USER_DATABASE.put("user1", "password1");
        USER_DATABASE.put("user2", "password2");
    }
    
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String password = credentials.get("password");
        
        // 验证用户名和密码
        if (username == null || password == null) {
            return ResponseEntity.badRequest().body(Map.of("message", "用户名和密码不能为空"));
        }
        
        // 检查用户是否存在且密码正确
        if (!USER_DATABASE.containsKey(username) || !USER_DATABASE.get(username).equals(password)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(Map.of("message", "用户名或密码错误"));
        }
        
        // 登录成功，返回成功响应
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "登录成功");
        response.put("username", username);
        
        return ResponseEntity.ok(response);
    }
}